package com.itbaizhan.shopping_user_service.service;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itbaizhan.shopping_common.exception.BusException;
import com.itbaizhan.shopping_common.pojo.ShoppingUser;
import com.itbaizhan.shopping_common.result.CodeEnum;
import com.itbaizhan.shopping_common.service.ShoppingUserService;
import com.itbaizhan.shopping_common.utils.JWTUtil;
import com.itbaizhan.shopping_common.utils.Md5Util;
import com.itbaizhan.shopping_user_service.mapper.UserMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author spf
 * @date 2024/1/10
 * @time 9:43
 */

// 前台用户服务
@Service
@Component
@Transactional
public class UserServiceImpl implements ShoppingUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void saveRegisterCheckCode(String phone, String checkCode) {
        ValueOperations opsForValue = redisTemplate.opsForValue();
        // redis键为手机号，值为验证码，过期时间为5分钟
        opsForValue.set("registerCode:" + phone, checkCode, 300, TimeUnit.SECONDS);
    }

    @Override
    public void registerCheckCode(String phone, String checkCode) {
        ValueOperations opsForValue = redisTemplate.opsForValue();
        Object value = opsForValue.get("registerCode:" + phone);
        if (!value.equals(checkCode)){
            throw new BusException(CodeEnum.REGISTER_CODE_ERROR);
        }
    }

    @Override
    public void register(ShoppingUser shoppingUser) {
        // 验证手机号是否存在
        String phone = shoppingUser.getPhone();
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        List<ShoppingUser> shoppingUsers = userMapper.selectList(queryWrapper);
        if (shoppingUsers != null && shoppingUsers.size() > 0){
            throw new BusException(CodeEnum.REGISTER_REPEAT_PHONE_ERROR);
        }
        // 验证用户名是否存在
        String username = shoppingUser.getUsername();
        QueryWrapper<ShoppingUser> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("username", username);
        List<ShoppingUser> shoppingUsers1 = userMapper.selectList(queryWrapper1);
        if (shoppingUsers1 != null && shoppingUsers1.size() > 0){
            throw new BusException(CodeEnum.REGISTER_REPEAT_USERNAME_ERROR);
        }
        // 添加到shopping用户表
        shoppingUser.setStatus("Y");
        // 密码加密
        shoppingUser.setPassword(Md5Util.encode(shoppingUser.getPassword()));
        userMapper.insert(shoppingUser);
    }

    @Override
    public String loginPassword(String username, String password) {
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        ShoppingUser shoppingUser = userMapper.selectOne(queryWrapper);

        // 验证用户名
        if (shoppingUser == null){
            throw new BusException(CodeEnum.LOGIN_NAME_PASSWORD_ERROR);
        }

        // 验证密码
        boolean verify = Md5Util.verify(password, shoppingUser.getPassword());
        if (!verify){
            throw new BusException(CodeEnum.LOGIN_NAME_PASSWORD_ERROR);
        }

        // return username;
        // 返回JWT token令牌信息
        String token = JWTUtil.sign(shoppingUser);
        return token;
    }

    @Override
    public void saveLoginCheckCode(String phone, String checkCode) {
        ValueOperations opsForValue = redisTemplate.opsForValue();
        opsForValue.set("loginCode:" + phone, checkCode, 300, TimeUnit.SECONDS);
    }

    @Override
    public String loginCheckCode(String phone, String checkCode) {
        ValueOperations opsForValue = redisTemplate.opsForValue();
        Object value = opsForValue.get("loginCode:" + phone);
        if (!checkCode.equals(value)){
            throw new BusException(CodeEnum.LOGIN_CODE_ERROR);
        }

        // 登录成功，查询用户
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        ShoppingUser shoppingUser = userMapper.selectOne(queryWrapper);
        // 返回用户名
        return shoppingUser.getUsername();
        // 实际返回token
    }

    @Override
    public String getName(String token) {
        String username = JWTUtil.verify(token);
        return username;
    }

    @Override
    public ShoppingUser getLoginUser(String token) {
        String username = JWTUtil.verify(token);
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        ShoppingUser shoppingUser = userMapper.selectOne(queryWrapper);
        return shoppingUser;
    }
}
